package helper

import (
	"github.com/slclub/go-tips"
	"regexp"
	"strconv"
)

func VerifyMobileFormatAny(cell interface{}) bool {
	if cell == nil {
		return false
	}
	if val, ok := cell.(int64); ok {
		return VerifyMobileFormat(val)
	}
	if val, ok := cell.(string); ok {
		v, _ := strconv.ParseInt(val, 10, 64)
		return VerifyMobileFormat(v)
	}
	return false
}

// 验证手机号
func VerifyMobileFormat(cell int64) bool {

	strPhone := strconv.FormatInt(cell, 10)
	regular := "^[0-9]{11}$"
	reg := regexp.MustCompile(regular)
	return reg.MatchString(strPhone)
}

func CheckWhiteCellWithDebug(cell_any any) bool {
	cnf := Config().Sub("SMS")
	if cnf.GetBool("IsDebug") {
		return true
	}
	var cell int64
	switch cell_ptr := cell_any.(type) {
	case string:
		cell, _ = strconv.ParseInt(cell_ptr, 10, 64)
	case int64:
		cell = int64(cell_ptr)
	case int:
		cell = int64(cell_ptr)
	default:
		return false
	}
	cells := cnf.GetStringSlice("white_cell")
	//if cell > 19999990000 && cell < 20000000000 {
	if len(cells) >= 2 && cell > tips.Any2Int64(cells[0]) && cell < tips.Any2Int64(cells[1]) {
		return true
	}

	if cells == nil || len(cells) == 0 {
		return false
	}
	for i, n := 0, len(cells); i < n; i++ {
		if tips.Any2Int64(cells[i]) == cell {
			return true
		}
	}
	return false
}
